/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-04-11 16:37:00
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-05-19 14:30:49
 */
import Koa from "koa";
import koaStatic from "koa-static";
import bodyParser from "koa-bodyparser";
import websockify from "koa-websocket"
import api from "./api";
import fs from "fs";
import path from "path";
import Game from "./common/game/Game";
import Conn from "./Conn";
import { initProto } from "./common/data/Proto";
import { initXlsxVo, loadData } from "./common/data/XlsxVo";
import * as kcoder from "k-coder"

//初始化
initProto(kcoder);
initXlsxVo(kcoder);
loadData(fs.readFileSync(path.resolve(__dirname, "../data/data.bin")));

/** 游戏实例 */
const game = new Game();
const port = 888
const app = websockify(new Koa());

app.use(bodyParser());
app.use(api.routes());
app.use(api.allowedMethods());
app.use(koaStatic(path.resolve(__dirname, "../public")));

app.ws.use(ctx => new Conn(game, ctx.websocket));

app.listen(port, () => {
    console.log("服务端已启动 http://localhost:" + port);
})